package org.springframework.web.servlet;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class DispatcherServlet extends HttpServlet {
//用来存放工厂对象
    static Map<String, Method> map=new HashMap<>();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws SecurityException, IOException{
        //获取用户的请求路径
        String uri=request.getRequestURI();
        String path=uri.substring(request.getContextPath().length());
        System.out.println("请求过来的path:"+path); //dev/HelloServlet
        //根据请求路径寻找执行的方法
        Method method = map.get(path);
        //执行方法
        //获取方法所在类的对象
        Class clz = method.getDeclaringClass();
        try {
            Object o = clz.newInstance();
            //获取方法参数列表
            Class[] parame = method.getParameterTypes();
            if (parame.length==0){
                method.invoke(o);
            }else if (parame.length==1){
                method.invoke(o,request);
            }else if (parame.length==2){
                method.invoke(o,request,response);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    protected void  doGet(HttpServletRequest request, HttpServletResponse
            response) throws ServletException,IOException{
        doPost(request,response);
    }


    @Override
    public void init(ServletConfig config) throws ServletException{
        //扫包指定包下的类
        String packageName="cn.bdqn.controller";
        //获取项目中的classes的路径
        String path = this.getClass().getClassLoader().getResource("/").getPath();
        path=path+(packageName.replace(".","/"));
        //根据路劲创建文件对象
        File file=new File(path.substring(1));
        //遍历文件夹下面所有的文件
        File[] files=file.listFiles();
        if (files !=null&&files.length>0){
            for (File file2 : file.listFiles()) {
                //获取文件名 类名。class
                String filename = file2.getName();
                //创建该文件所对应类的对象
                //cn.bdqn.controller.UserController
                try {
                    Class clz = Class.forName(packageName + "." + filename.substring(0, filename.indexOf(".")));
                    Method[] declaredMethods = clz.getDeclaredMethods();
                    for (Method method : declaredMethods) {
                        if (method.isAnnotationPresent(RequestMapping.class)){
                                //获取注解对象
                            RequestMapping annotation =(RequestMapping) method.getAnnotation(RequestMapping.class);
                            map.put(annotation.value(),method);
                            System.out.println("存入的map中的数据："+annotation.value());
                        }
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                //获取该类中所有方法
            }
        }
    }
}
